#include "money-tree.hpp"
#include "params.hpp"
#include "tick.h"
#include "types.h"
#include "udp.h"
#include <cstdint>
#include <cstdio>
#include <fcntl.h> /* For O_* constants */
#include <memory.h>
#include <stdint.h>
#include <string>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <unistd.h>

extern price_t g_hop;
extern UdpClient* g_uc;

int
main(int argc, char** argv)
{
  if (argc != 4) {
    printf("Usage %s <shared memory key> <start-idx> <debug|nodebug|step>\n",
           argv[0]);
    return (0);
  }

  TICK_DATA td = nullptr;
  size_t td_size = 0;
  Trade::Params* vo = nullptr;
  size_t vo_size = 0;

  if (load_data_from_shared_memory(argv[1], &td, &td_size, &vo, &vo_size)) {
    printf("Load shared memory data error\n");
    return -1;
  }
  g_uc = init_udp_client("127.0.0.1", "34567");  
  
  int64_t start_idx = std::stoll(argv[2]);
  Trade::MoneyTree mt;
  mt.set_data(td, td_size, vo, vo_size);
  mt.set_debug(argv[3]);
  mt.set_strategy(0);
  int ret = mt.make_deal(start_idx);

  close_udp_client(g_uc);
  Trade::release_shared_memory(argv[1], (void*)(td), td_size, vo_size);

  return ret;
}
